package org.xinjiang.tarim.travel.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xinjiang.tarim.travel.model.*;
import org.xinjiang.tarim.travel.service.RouteService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.utils.SensitiveWord;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/route")
public class RouteApi {
    @Autowired
    RouteService routeService;



    /**
     * 创建行程
     *
     * @param request
     * @param param
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, @RequestBody String param) {
        JsonResult jsonResult;
        String openid = request.getHeader("tokenId");
        Map map = JSON.parseObject(param, Map.class);
        map = JSON.parseObject(map.get("param").toString(), Map.class);
        RouteWithBLOBs route = JSON.parseObject(JSON.toJSONString(map.get("route")), RouteWithBLOBs.class);
        List<PoiMdd> poiMddList = JSONArray.parseArray(JSON.toJSONString(map.get("poiMddList")), PoiMdd.class);
        route.setOpenid(openid);
        Long routeId = routeService.insert(route, poiMddList);
        if (routeId == null) {
            jsonResult = new JsonResult(Constants.STATUS_FAIL, "创建行程失败");
        } else {
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "创建行程成功");
        }
        return jsonResult;
    }

    /**
     * 根据用户查询（分页）
     *
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectByOpenid")
    public JsonResult selectByOpenid(HttpServletRequest request,
                                     @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        JsonResult jsonResult;
        try {
            String openid = request.getHeader("tokenId");
            PageBean pageBean = new PageBean(pageIndex, pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("openid", openid);
            pageBean = routeService.selectByOpenid(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 获取行程详情
     *
     * @param routeId
     * @return
     */
    @RequestMapping(value = "/getDetail")
    public JsonResult getDetail(Long routeId) {
        JsonResult jsonResult;
        try {
            Map map = routeService.selectByRouteId(routeId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "获取成功", map);
        } catch (Exception e) {
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "获取异常");
        }
        return jsonResult;
    }

    /**
     * 设置行程封面
     *
     * @param route
     * @return
     */
    @RequestMapping(value = "/setCover")
    public JsonResult setCover(RouteWithBLOBs route) {
        JsonResult jsonResult;
        try {
            int i = routeService.updateCoverImage(route);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "设置异常");
        }
        return jsonResult;
    }

    /**
     * 设置行程
     *
     * @param route
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResult update(@RequestBody RouteWithBLOBs route) {
        JsonResult jsonResult;
        try {
            int i = routeService.updateByPrimaryKeySelective(route);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "保存异常");
        }
        return jsonResult;
    }

    /**
     * 删除行程
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    public JsonResult delete(@RequestParam(value = "id") Long id) {
        JsonResult jsonResult;
        try {
            int i = routeService.deleteByPrimaryKey(id);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "删除异常");
        }
        return jsonResult;
    }

    /**
     * 发布行程
     * @param param
     * @return
     */
    @RequestMapping(value = "/release", method = RequestMethod.POST)
    public JsonResult release(@RequestBody String param) {
        JsonResult jsonResult;
        try {
            SensitiveWord sensitiveWord = new SensitiveWord();
            sensitiveWord.InitializationWork();
            Map map = JSON.parseObject(param, Map.class);
            map = JSON.parseObject(map.get("param").toString(), Map.class);
            RouteWithBLOBs route = JSON.parseObject(map.get("route").toString(), RouteWithBLOBs.class);
            route.setContent(sensitiveWord.filterInfo(route.getContent()));
            List<RouteDay> list = JSONArray.parseArray(map.get("routeDays").toString(), RouteDay.class);
            for (RouteDay routeDay : list){
                routeDay.setPlanContent(sensitiveWord.filterInfo(routeDay.getPlanContent()));
            }
            int i = routeService.updateForRelease(route, list);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "发布异常");
        }
        return jsonResult;
    }

    /**
     * 发布（改）
     * @param route
     * @return
     */
    @RequestMapping(value = "/release2")
    public JsonResult release(@RequestBody RouteWithBLOBs route){
        JsonResult jsonResult;
        try {
            int i = routeService.updateForRelease(route);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "发布成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 查询推荐
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getRecommend")
    public JsonResult getRecomment(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        JsonResult jsonResult;
        try {
            PageBean pageBean = new PageBean(pageIndex,pageSize);
            Criteria criteria = new Criteria();
            pageBean = routeService.selectRecommend(criteria, pageBean);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }
}
